﻿using Hong.views;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Hong.viewmodels
{
    public class LoginViewModel : INotifyPropertyChanged, ICommand
    {
        public event PropertyChangedEventHandler? PropertyChanged;
        public event EventHandler? CanExecuteChanged;

        public void NotifyChanged([CallerMemberName] string propName = "")
        { 
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
        }

        public bool CanExecute(object? parameter)
        {
            return true;
        }

        public void Execute(object? parameter)
        {
            string? cmd = parameter as string;
            if (cmd != null && cmd == "close")
            {
                Application.Current.Shutdown();
            }
            else if(parameter is LoginView)
            {
                var win = parameter as LoginView;
                if (win == null) return;

                if(this.UserName.Length < 1)
                {
                    this.ErrorMessage = "用户名为空";
                    return;
                }
                if (this.Password.Length < 1)
                {
                    this.ErrorMessage = "密码为空";
                    return;
                }
                win.DialogResult = true;
            }
        }

        private string _userName = "admin";

        public string UserName
        {
            get { return _userName; }
            set { _userName = value; this.NotifyChanged(); }
        }

        private string _password = "123";

        public string Password
        {
            get { return _password; }
            set { _password = value; this.NotifyChanged(); }
        }

        private string _errorMessage = "";

        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { _errorMessage = value; this.NotifyChanged(); }
        }

    }
}
